package com.example.smartcity_poverty.ui.AllService.SmartBus.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.context
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.ServiceBean.SmartBus.BusPathAllModel
import com.example.smartcity_poverty.logic.util.Util
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.SmartBus.adapter.BusHomeRecyclerAdapter
import kotlinx.android.synthetic.main.activity_smart_bus_one.*
import kotlinx.android.synthetic.main.re_bus_home_recycler.*
import kotlinx.coroutines.Dispatchers

/**
 * 第一步页面：显示站点路线秒点地图
 */
class SmartBusOneActivity : AppCompatActivity() {

    private val id by lazy {
        intent.getStringExtra("id")?.toInt()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_smart_bus_one)
        title = "站点路线"

        Util.addActivity(this)

        // 获取数据填充
        coroutine(Dispatchers.IO) {
            val result = api.getBusPathDataIls(id!!)
            val result1 = api.getBusPathAll(id!!)
            if (result.code == 200) {

                // 数据填充
                runOnUiThread {
                    result.data?.let {
                        busOneStartTv.text = it.first
                        busOneEndTv.text = it.end
                        busOneQiAn.text = "价格: ${it.price}"
                        busOneThis.text = "里程: ${it.mileage}公里"
                    }

                    val array = ArrayList<BusPathAllModel.RowsBean>()
                    if (result.code == 200) {
                        result1.rows?.let {
                            array.addAll(it)
                        }
                        // 配置适配器
                        busOneRv.apply {
                            this.layoutManager = LinearLayoutManager(context).apply {
                                this.orientation = LinearLayoutManager.HORIZONTAL
                            }
                            this.adapter =
                                BusHomeRecyclerAdapter.BusAll(array, R.layout.re_bus_home_all)
                        }
                    }
                }

            } else runOnUiThread { result.msg?.show() }
        }


        // 点击进入下一步
        busOneOk.setOnClickListener {
            startActivity(
                Intent(this, SmartCityTwoActivity::class.java).putExtra(
                    "id",
                    id.toString()
                )
            )
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}